﻿using JewererBasic.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Transactions;
using System.Web;

namespace JewererBasic.BO
{
    public class Component_Order_BO
    {
        public static COMPONENT_ORDER GetTodayComponentOrder()
        {
            MSQDBEntities db = new MSQDBEntities();
            COMPONENT_ORDER result=(from d in db.COMPONENT_ORDER
                         where d.DateMake.Day==DateTime.Now.Day 
                         && d.DateMake.Month==DateTime.Now.Month
                         && d.DateMake.Year==DateTime.Now.Year
                         select d).FirstOrDefault();
            if (result == null)
            {
                result = new COMPONENT_ORDER();
                result.DateMake = DateTime.Now;
                result.MC = CurrentUser.UserID;
                db.COMPONENT_ORDER.Add(result);
                db.SaveChanges();
            }
            return result;
        }

        public static bool AddComponentOrderDetail(List<COMPONENT_ORDER_DETAIL> models)
        {
            using (var scope = new TransactionScope())
            {
                MSQDBEntities db = new MSQDBEntities();
                COMPONENT_ORDER today_order = GetTodayComponentOrder();

                for (int i = 0; i < models.Count; i++)
                {
                    models[i].Component_Order_Id = today_order.Id;
                    models[i].Return_Weight = 0;
                    models[i].Finish = -1; // chua bat dau
                    List<COMPONENT_ORDER_COM_MOLECULE_EXPORT> tem = models[i].COMPONENT_ORDER_COM_MOLECULE_EXPORT.ToList();
                    models[i].COMPONENT_ORDER_COM_MOLECULE_EXPORT = null;
                    db.COMPONENT_ORDER_DETAIL.Add(models[i]);
                    db.SaveChanges();
                    
                    for (int j = 0; j < tem.Count; j++)
                    {
                        tem[j].Com_Order_Detail_Id = models[i].Id;
                        tem[j].TimeMake = DateTime.Now;
                        tem[j].IsExported = 0; // has not exported
                        db.COMPONENT_ORDER_COM_MOLECULE_EXPORT.Add(tem[j]);
                        db.SaveChanges();
                    }
                }
                scope.Complete();
            }
            return true;
        }

        public static COMPONENT_ORDER_DETAIL GetComponentOrderDetail(int id)
        {
            MSQDBEntities db = new MSQDBEntities();
            COMPONENT_ORDER_DETAIL result = db.COMPONENT_ORDER_DETAIL.Find(id);
            return result;
        }

        public static bool DeleteComponentOrderDetail(int id)
        {
            MSQDBEntities db = new MSQDBEntities();
            COMPONENT_ORDER_DETAIL com_order_detail = db.COMPONENT_ORDER_DETAIL.Find(id);
            if (com_order_detail.Finish == -1)// chua bat dau
            {
                var tem = com_order_detail.COMPONENT_ORDER_COM_MOLECULE_EXPORT; ;
                while(tem.Count>0)
                {
                    db.COMPONENT_ORDER_COM_MOLECULE_EXPORT.Remove(tem.First());
                }
                db.SaveChanges();
                db.COMPONENT_ORDER_DETAIL.Remove(com_order_detail);
                db.SaveChanges();
                return true;
            }
            return false;
        }

        public static bool Update(COMPONENT_ORDER_DETAIL model)
        {
            MSQDBEntities db=new MSQDBEntities();
            COMPONENT_ORDER_DETAIL component_order_detail = db.COMPONENT_ORDER_DETAIL.Find(model.Id);
            if (component_order_detail != null)
            {
                if (!string.Equals(component_order_detail.Worker_Id, model.Worker_Id))
                {
                    component_order_detail.Worker_Id = model.Worker_Id;
                    db.SaveChanges();
                }
                foreach (var item in model.COMPONENT_ORDER_COM_MOLECULE_EXPORT)
                {
                    COMPONENT_ORDER_COM_MOLECULE_EXPORT tem = db.COMPONENT_ORDER_COM_MOLECULE_EXPORT.Find(item.Id);
                    if (tem.Export_Weight != item.Export_Weight)
                    {
                        tem.Export_Weight = Math.Round(item.Export_Weight, 3);
                        db.SaveChanges();
                    }
                }
            }
            return true;
        }

        public static bool Finish(COMPONENT_ORDER_DETAIL model)
        {
            MSQDBEntities db = new MSQDBEntities();
            COMPONENT_ORDER_DETAIL component_order_detail = db.COMPONENT_ORDER_DETAIL.Find(model.Id);
            if (component_order_detail != null)
            {
                component_order_detail.Finish = 1;
                component_order_detail.Return_Weight = Math.Round(model.Return_Weight, 3);
                component_order_detail.Dust = model.Dust;
                component_order_detail.COMPONENT.Weight += model.Return_Weight;
                Math.Round(component_order_detail.COMPONENT.Weight.Value, 3);
                // Fix
                //component_order_detail.COMPONENT.Dust += model.Dust;
                if(component_order_detail.COMPONENT.Dust==null)
                    component_order_detail.COMPONENT.Dust = model.Dust;
                else
                    component_order_detail.COMPONENT.Dust += model.Dust;
                // End Fix

                Math.Round(component_order_detail.COMPONENT.Dust.Value, 3);
                
                // Sang Add
                Waste_Report_BO.Update_Worker_Waste(component_order_detail.Worker_Id, component_order_detail);
                //End Add

                db.SaveChanges();
                return true;
            }
            return false;
        }

        public static bool Export(int component_order_detail_id)
        {
            using (var scope = new TransactionScope())
            {
                try
                {
                    MSQDBEntities db = new MSQDBEntities();
                    COMPONENT_ORDER_DETAIL component_order_detail = db.COMPONENT_ORDER_DETAIL.Find(component_order_detail_id);
                    if (component_order_detail != null)
                    {
                        // update component_order_detail
                        component_order_detail.Finish = 0;
                        db.SaveChanges();

                        // update molecule
                        var tem = component_order_detail.COMPONENT_ORDER_COM_MOLECULE_EXPORT;
                        for (int i = 0; i < tem.Count; i++)
                        {
                            COMPONENT_ORDER_COM_MOLECULE_EXPORT molecule = tem.ElementAt(i);
                            molecule.IsExported = 1;

                            // tru trong bang component
                            COMPONENT com = db.COMPONENTs.Find(molecule.Com_Slaver_Id);
                            if (com != null)
                            {
                                if (com.Weight.HasValue)
                                {
                                    com.Weight = Math.Round((com.Weight.Value - molecule.Export_Weight), 3);
                                }
                                db.SaveChanges();

                                // Tang trong bang thanh phan component
                                COMPONENT_MOLECULE thanhphan = db.COMPONENT_MOLECULE.Where(x => string.Equals(x.MainComponentId, component_order_detail.Component_Id) && string.Equals(molecule.Com_Slaver_Id, x.SlaveComponentId)).FirstOrDefault();
                                if (thanhphan == null)// Neu khong tim thay thi add them trong bang thanh phan
                                {
                                    thanhphan = new COMPONENT_MOLECULE();
                                    thanhphan.MainComponentId = component_order_detail.Component_Id;
                                    thanhphan.SlaveComponentId = molecule.Com_Slaver_Id;
                                    thanhphan.Total_Export_Weight = 0;
                                    db.COMPONENT_MOLECULE.Add(thanhphan);
                                }
                                thanhphan.Total_Export_Weight = Math.Round(molecule.Export_Weight + thanhphan.Total_Export_Weight, 3);
                                db.SaveChanges();
                            }
                            else
                                return false;
                        }
                        scope.Complete();
                        return true;
                    }
                    return false;
                }
                catch
                {
                    return false;
                }                
            }
        }
    }
}